<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>containerrenderer.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_containerrenderer.js.html">containerrenderer.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Base class for container renderers.
<a name="line17"></a> *
<a name="line18"></a> * @author attila@google.com (Attila Bodis)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.ui.ContainerRenderer&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line24"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom.classes&#39;);
<a name="line28"></a>goog.require(&#39;goog.string&#39;);
<a name="line29"></a>goog.require(&#39;goog.style&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Separator&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line32"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * Default renderer for {@link goog.ui.Container}.  Can be used as-is, but
<a name="line38"></a> * subclasses of Container will probably want to use renderers specifically
<a name="line39"></a> * tailored for them by extending this class.
<a name="line40"></a> * @constructor
<a name="line41"></a> */
<a name="line42"></a>goog.ui.ContainerRenderer = function() {
<a name="line43"></a>};
<a name="line44"></a>goog.addSingletonGetter(goog.ui.ContainerRenderer);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Constructs a new renderer and sets the CSS class that the renderer will use
<a name="line49"></a> * as the base CSS class to apply to all elements rendered by that renderer.
<a name="line50"></a> * An example to use this function using a menu is:
<a name="line51"></a> *
<a name="line52"></a> * &lt;pre&gt;
<a name="line53"></a> * var myCustomRenderer = goog.ui.ContainerRenderer.getCustomRenderer(
<a name="line54"></a> *     goog.ui.MenuRenderer, &#39;my-special-menu&#39;);
<a name="line55"></a> * var newMenu = new goog.ui.Menu(opt_domHelper, myCustomRenderer);
<a name="line56"></a> * &lt;/pre&gt;
<a name="line57"></a> *
<a name="line58"></a> * Your styles for the menu can now be:
<a name="line59"></a> * &lt;pre&gt;
<a name="line60"></a> * .my-special-menu { }
<a name="line61"></a> * &lt;/pre&gt;
<a name="line62"></a> *
<a name="line63"></a> * &lt;em&gt;instead&lt;/em&gt; of
<a name="line64"></a> * &lt;pre&gt;
<a name="line65"></a> * .CSS_MY_SPECIAL_MENU .goog-menu { }
<a name="line66"></a> * &lt;/pre&gt;
<a name="line67"></a> *
<a name="line68"></a> * You would want to use this functionality when you want an instance of a
<a name="line69"></a> * component to have specific styles different than the other components of the
<a name="line70"></a> * same type in your application.  This avoids using descendant selectors to
<a name="line71"></a> * apply the specific styles to this component.
<a name="line72"></a> *
<a name="line73"></a> * @param {Function} ctor The constructor of the renderer you want to create.
<a name="line74"></a> * @param {string} cssClassName The name of the CSS class for this renderer.
<a name="line75"></a> * @return {goog.ui.ContainerRenderer} An instance of the desired renderer with
<a name="line76"></a> *     its getCssClass() method overridden to return the supplied custom CSS
<a name="line77"></a> *     class name.
<a name="line78"></a> */
<a name="line79"></a>goog.ui.ContainerRenderer.getCustomRenderer = function(ctor, cssClassName) {
<a name="line80"></a>  var renderer = new ctor();
<a name="line81"></a>
<a name="line82"></a>  /**
<a name="line83"></a>   * Returns the CSS class to be applied to the root element of components
<a name="line84"></a>   * rendered using this renderer.
<a name="line85"></a>   * @return {string} Renderer-specific CSS class.
<a name="line86"></a>   */
<a name="line87"></a>  renderer.getCssClass = function() {
<a name="line88"></a>    return cssClassName;
<a name="line89"></a>  };
<a name="line90"></a>
<a name="line91"></a>  return renderer;
<a name="line92"></a>};
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Default CSS class to be applied to the root element of containers rendered
<a name="line97"></a> * by this renderer.
<a name="line98"></a> * @type {string}
<a name="line99"></a> */
<a name="line100"></a>goog.ui.ContainerRenderer.CSS_CLASS = goog.getCssName(&#39;goog-container&#39;);
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * Returns the ARIA role to be applied to the container.
<a name="line105"></a> * See http://wiki/Main/ARIA for more info.
<a name="line106"></a> * @return {undefined|string} ARIA role.
<a name="line107"></a> */
<a name="line108"></a>goog.ui.ContainerRenderer.prototype.getAriaRole = function() {
<a name="line109"></a>  // By default, the ARIA role is unspecified.
<a name="line110"></a>  return undefined;
<a name="line111"></a>};
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Enables or disables the tab index of the element.  Only elements with a
<a name="line116"></a> * valid tab index can receive focus.
<a name="line117"></a> * @param {Element} element Element whose tab index is to be changed.
<a name="line118"></a> * @param {boolean} enable Whether to add or remove the element&#39;s tab index.
<a name="line119"></a> */
<a name="line120"></a>goog.ui.ContainerRenderer.prototype.enableTabIndex = function(element, enable) {
<a name="line121"></a>  if (element) {
<a name="line122"></a>    element.tabIndex = enable ? 0 : -1;
<a name="line123"></a>  }
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Creates and returns the container&#39;s root element.  The default
<a name="line129"></a> * simply creates a DIV and applies the renderer&#39;s own CSS class name to it.
<a name="line130"></a> * To be overridden in subclasses.
<a name="line131"></a> * @param {goog.ui.Container} container Container to render.
<a name="line132"></a> * @return {Element} Root element for the container.
<a name="line133"></a> */
<a name="line134"></a>goog.ui.ContainerRenderer.prototype.createDom = function(container) {
<a name="line135"></a>  return container.getDomHelper().createDom(&#39;div&#39;,
<a name="line136"></a>      this.getClassNames(container).join(&#39; &#39;));
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Returns the DOM element into which child components are to be rendered,
<a name="line142"></a> * or null if the container hasn&#39;t been rendered yet.
<a name="line143"></a> * @param {Element} element Root element of the container whose content element
<a name="line144"></a> *     is to be returned.
<a name="line145"></a> * @return {Element} Element to contain child elements (null if none).
<a name="line146"></a> */
<a name="line147"></a>goog.ui.ContainerRenderer.prototype.getContentElement = function(element) {
<a name="line148"></a>  return element;
<a name="line149"></a>};
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Default implementation of {@code canDecorate}; returns true if the element
<a name="line154"></a> * is a DIV, false otherwise.
<a name="line155"></a> * @param {Element} element Element to decorate.
<a name="line156"></a> * @return {boolean} Whether the renderer can decorate the element.
<a name="line157"></a> */
<a name="line158"></a>goog.ui.ContainerRenderer.prototype.canDecorate = function(element) {
<a name="line159"></a>  return element.tagName == &#39;DIV&#39;;
<a name="line160"></a>};
<a name="line161"></a>
<a name="line162"></a>
<a name="line163"></a>/**
<a name="line164"></a> * Default implementation of {@code decorate} for {@link goog.ui.Container}s.
<a name="line165"></a> * Decorates the element with the container, and attempts to decorate its child
<a name="line166"></a> * elements.  Returns the decorated element.
<a name="line167"></a> * @param {goog.ui.Container} container Container to decorate the element.
<a name="line168"></a> * @param {Element} element Element to decorate.
<a name="line169"></a> * @return {Element} Decorated element.
<a name="line170"></a> */
<a name="line171"></a>goog.ui.ContainerRenderer.prototype.decorate = function(container, element) {
<a name="line172"></a>  // Set the container&#39;s ID to the decorated element&#39;s DOM ID, if any.
<a name="line173"></a>  if (element.id) {
<a name="line174"></a>    container.setId(element.id);
<a name="line175"></a>  }
<a name="line176"></a>
<a name="line177"></a>  // Configure the container&#39;s state based on the CSS class names it has.
<a name="line178"></a>  var baseClass = this.getCssClass();
<a name="line179"></a>  var hasBaseClass = false;
<a name="line180"></a>  var classNames = goog.dom.classes.get(element);
<a name="line181"></a>  if (classNames) {
<a name="line182"></a>    goog.array.forEach(classNames, function(className) {
<a name="line183"></a>      if (className == baseClass) {
<a name="line184"></a>        hasBaseClass = true;
<a name="line185"></a>      } else if (className) {
<a name="line186"></a>        this.setStateFromClassName(container, className, baseClass);
<a name="line187"></a>      }
<a name="line188"></a>    }, this);
<a name="line189"></a>  }
<a name="line190"></a>
<a name="line191"></a>  if (!hasBaseClass) {
<a name="line192"></a>    // Make sure the container&#39;s root element has the renderer&#39;s own CSS class.
<a name="line193"></a>    goog.dom.classes.add(element, baseClass);
<a name="line194"></a>  }
<a name="line195"></a>
<a name="line196"></a>  // Decorate the element&#39;s children, if applicable.  This should happen after
<a name="line197"></a>  // the container&#39;s own state has been initialized, since how children are
<a name="line198"></a>  // decorated may depend on the state of the container.
<a name="line199"></a>  this.decorateChildren(container, this.getContentElement(element));
<a name="line200"></a>
<a name="line201"></a>  return element;
<a name="line202"></a>};
<a name="line203"></a>
<a name="line204"></a>
<a name="line205"></a>/**
<a name="line206"></a> * Sets the container&#39;s state based on the given CSS class name, encountered
<a name="line207"></a> * during decoration.  CSS class names that don&#39;t represent container states
<a name="line208"></a> * are ignored.  Considered protected; subclasses should override this method
<a name="line209"></a> * to support more states and CSS class names.
<a name="line210"></a> * @param {goog.ui.Container} container Container to update.
<a name="line211"></a> * @param {string} className CSS class name.
<a name="line212"></a> * @param {string} baseClass Base class name used as the root of state-specific
<a name="line213"></a> *     class names (typically the renderer&#39;s own class name).
<a name="line214"></a> * @protected
<a name="line215"></a> */
<a name="line216"></a>goog.ui.ContainerRenderer.prototype.setStateFromClassName = function(container,
<a name="line217"></a>    className, baseClass) {
<a name="line218"></a>  if (className == goog.getCssName(baseClass, &#39;disabled&#39;)) {
<a name="line219"></a>    container.setEnabled(false);
<a name="line220"></a>  } else if (className == goog.getCssName(baseClass, &#39;horizontal&#39;)) {
<a name="line221"></a>    container.setOrientation(goog.ui.Container.Orientation.HORIZONTAL);
<a name="line222"></a>  } else if (className == goog.getCssName(baseClass, &#39;vertical&#39;)) {
<a name="line223"></a>    container.setOrientation(goog.ui.Container.Orientation.VERTICAL);
<a name="line224"></a>  }
<a name="line225"></a>};
<a name="line226"></a>
<a name="line227"></a>
<a name="line228"></a>/**
<a name="line229"></a> * Takes a container and an element that may contain child elements, decorates
<a name="line230"></a> * the child elements, and adds the corresponding components to the container
<a name="line231"></a> * as child components.  Any non-element child nodes (e.g. empty text nodes
<a name="line232"></a> * introduced by line breaks in the HTML source) are removed from the element.
<a name="line233"></a> * @param {goog.ui.Container} container Container whose children are to be
<a name="line234"></a> *     discovered.
<a name="line235"></a> * @param {Element} element Element whose children are to be decorated.
<a name="line236"></a> * @param {Element=} opt_firstChild the first child to be decorated.
<a name="line237"></a> * @suppress {visibility} setElementInternal
<a name="line238"></a> */
<a name="line239"></a>goog.ui.ContainerRenderer.prototype.decorateChildren = function(container,
<a name="line240"></a>    element, opt_firstChild) {
<a name="line241"></a>  if (element) {
<a name="line242"></a>    var node = opt_firstChild || element.firstChild, next;
<a name="line243"></a>    // Tag soup HTML may result in a DOM where siblings have different parents.
<a name="line244"></a>    while (node &amp;&amp; node.parentNode == element) {
<a name="line245"></a>      // Get the next sibling here, since the node may be replaced or removed.
<a name="line246"></a>      next = node.nextSibling;
<a name="line247"></a>      if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line248"></a>        // Decorate element node.
<a name="line249"></a>        var child = this.getDecoratorForChild(/** @type {Element} */(node));
<a name="line250"></a>        if (child) {
<a name="line251"></a>          // addChild() may need to look at the element.
<a name="line252"></a>          child.setElementInternal(/** @type {Element} */(node));
<a name="line253"></a>          // If the container is disabled, mark the child disabled too.  See
<a name="line254"></a>          // bug 1263729.  Note that this must precede the call to addChild().
<a name="line255"></a>          if (!container.isEnabled()) {
<a name="line256"></a>            child.setEnabled(false);
<a name="line257"></a>          }
<a name="line258"></a>          container.addChild(child);
<a name="line259"></a>          child.decorate(/** @type {Element} */(node));
<a name="line260"></a>        }
<a name="line261"></a>      } else if (!node.nodeValue || goog.string.trim(node.nodeValue) == &#39;&#39;) {
<a name="line262"></a>        // Remove empty text node, otherwise madness ensues (e.g. controls that
<a name="line263"></a>        // use goog-inline-block will flicker and shift on hover on Gecko).
<a name="line264"></a>        element.removeChild(node);
<a name="line265"></a>      }
<a name="line266"></a>      node = next;
<a name="line267"></a>    }
<a name="line268"></a>  }
<a name="line269"></a>};
<a name="line270"></a>
<a name="line271"></a>
<a name="line272"></a>/**
<a name="line273"></a> * Inspects the element, and creates an instance of {@link goog.ui.Control} or
<a name="line274"></a> * an appropriate subclass best suited to decorate it.  Returns the control (or
<a name="line275"></a> * null if no suitable class was found).  This default implementation uses the
<a name="line276"></a> * element&#39;s CSS class to find the appropriate control class to instantiate.
<a name="line277"></a> * May be overridden in subclasses.
<a name="line278"></a> * @param {Element} element Element to decorate.
<a name="line279"></a> * @return {goog.ui.Control?} A new control suitable to decorate the element
<a name="line280"></a> *     (null if none).
<a name="line281"></a> */
<a name="line282"></a>goog.ui.ContainerRenderer.prototype.getDecoratorForChild = function(element) {
<a name="line283"></a>  return /** @type {goog.ui.Control} */ (
<a name="line284"></a>      goog.ui.registry.getDecorator(element));
<a name="line285"></a>};
<a name="line286"></a>
<a name="line287"></a>
<a name="line288"></a>/**
<a name="line289"></a> * Initializes the container&#39;s DOM when the container enters the document.
<a name="line290"></a> * Called from {@link goog.ui.Container#enterDocument}.
<a name="line291"></a> * @param {goog.ui.Container} container Container whose DOM is to be initialized
<a name="line292"></a> *     as it enters the document.
<a name="line293"></a> */
<a name="line294"></a>goog.ui.ContainerRenderer.prototype.initializeDom = function(container) {
<a name="line295"></a>  var elem = container.getElement();
<a name="line296"></a>  goog.asserts.assert(elem, &#39;The container DOM element cannot be null.&#39;);
<a name="line297"></a>  // Make sure the container&#39;s element isn&#39;t selectable.  On Gecko, recursively
<a name="line298"></a>  // marking each child element unselectable is expensive and unnecessary, so
<a name="line299"></a>  // only mark the root element unselectable.
<a name="line300"></a>  goog.style.setUnselectable(elem, true, goog.userAgent.GECKO);
<a name="line301"></a>
<a name="line302"></a>  // IE doesn&#39;t support outline:none, so we have to use the hideFocus property.
<a name="line303"></a>  if (goog.userAgent.IE) {
<a name="line304"></a>    elem.hideFocus = true;
<a name="line305"></a>  }
<a name="line306"></a>
<a name="line307"></a>  // Set the ARIA role.
<a name="line308"></a>  var ariaRole = this.getAriaRole();
<a name="line309"></a>  if (ariaRole) {
<a name="line310"></a>    goog.a11y.aria.setRole(elem, ariaRole);
<a name="line311"></a>  }
<a name="line312"></a>};
<a name="line313"></a>
<a name="line314"></a>
<a name="line315"></a>/**
<a name="line316"></a> * Returns the element within the container&#39;s DOM that should receive keyboard
<a name="line317"></a> * focus (null if none).  The default implementation returns the container&#39;s
<a name="line318"></a> * root element.
<a name="line319"></a> * @param {goog.ui.Container} container Container whose key event target is
<a name="line320"></a> *     to be returned.
<a name="line321"></a> * @return {Element} Key event target (null if none).
<a name="line322"></a> */
<a name="line323"></a>goog.ui.ContainerRenderer.prototype.getKeyEventTarget = function(container) {
<a name="line324"></a>  return container.getElement();
<a name="line325"></a>};
<a name="line326"></a>
<a name="line327"></a>
<a name="line328"></a>/**
<a name="line329"></a> * Returns the CSS class to be applied to the root element of containers
<a name="line330"></a> * rendered using this renderer.
<a name="line331"></a> * @return {string} Renderer-specific CSS class.
<a name="line332"></a> */
<a name="line333"></a>goog.ui.ContainerRenderer.prototype.getCssClass = function() {
<a name="line334"></a>  return goog.ui.ContainerRenderer.CSS_CLASS;
<a name="line335"></a>};
<a name="line336"></a>
<a name="line337"></a>
<a name="line338"></a>/**
<a name="line339"></a> * Returns all CSS class names applicable to the given container, based on its
<a name="line340"></a> * state.  The array of class names returned includes the renderer&#39;s own CSS
<a name="line341"></a> * class, followed by a CSS class indicating the container&#39;s orientation,
<a name="line342"></a> * followed by any state-specific CSS classes.
<a name="line343"></a> * @param {goog.ui.Container} container Container whose CSS classes are to be
<a name="line344"></a> *     returned.
<a name="line345"></a> * @return {Array.&lt;string&gt;} Array of CSS class names applicable to the
<a name="line346"></a> *     container.
<a name="line347"></a> */
<a name="line348"></a>goog.ui.ContainerRenderer.prototype.getClassNames = function(container) {
<a name="line349"></a>  var baseClass = this.getCssClass();
<a name="line350"></a>  var isHorizontal =
<a name="line351"></a>      container.getOrientation() == goog.ui.Container.Orientation.HORIZONTAL;
<a name="line352"></a>  var classNames = [
<a name="line353"></a>    baseClass,
<a name="line354"></a>    (isHorizontal ?
<a name="line355"></a>        goog.getCssName(baseClass, &#39;horizontal&#39;) :
<a name="line356"></a>        goog.getCssName(baseClass, &#39;vertical&#39;))
<a name="line357"></a>  ];
<a name="line358"></a>  if (!container.isEnabled()) {
<a name="line359"></a>    classNames.push(goog.getCssName(baseClass, &#39;disabled&#39;));
<a name="line360"></a>  }
<a name="line361"></a>  return classNames;
<a name="line362"></a>};
<a name="line363"></a>
<a name="line364"></a>
<a name="line365"></a>/**
<a name="line366"></a> * Returns the default orientation of containers rendered or decorated by this
<a name="line367"></a> * renderer.  The base class implementation returns {@code VERTICAL}.
<a name="line368"></a> * @return {goog.ui.Container.Orientation} Default orientation for containers
<a name="line369"></a> *     created or decorated by this renderer.
<a name="line370"></a> */
<a name="line371"></a>goog.ui.ContainerRenderer.prototype.getDefaultOrientation = function() {
<a name="line372"></a>  return goog.ui.Container.Orientation.VERTICAL;
<a name="line373"></a>};
</pre>


</body>
</html>
